package algotithm.weekendrace.leetcode240.test2;

/**
 * @author 25068
 * @Date 2021/5/9 21:41
 * @Title 5751. 下标对中的最大距离
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {55,30,5,4,2};
        int[] nums2 = {100,20,10,10,5};
        int maxDistance = solution.maxDistance(nums1, nums2);
        System.out.println(maxDistance);
    }

    public int maxDistance(int[] nums1, int[] nums2) {
        int ans = 0;
        for(int i = 0; i < nums1.length; i++) {
            int j = bs(nums2, i, nums2.length-1, nums1[i]);
            ans = Math.max(ans, j - i);
        }
        return ans;
    }

    // first >= t
    private int bs(int[] nums, int l, int r, int t) {
        while(l < r) {
            int m = (l + r + 1) / 2;
            if(nums[m] >= t) {
                l = m;
            } else {
                r = m - 1;
            }
        }

        return l;
    }
}